from models.records import Record
from models.clockins import Clockin
from models.tags import Tag
from models.tasks import Task
from models.timetables import Timetable
from math import ceil
from flask_restx import Resource
from webargs.flaskparser import use_args
from webargs import fields
from schemas.pagination import PaginationSchema



models = {'record':Record,
        'tag':Tag,
        'task':Task,
        'timetable':Timetable,
        'clockin':Clockin}

class PaginationResource(Resource):
    
    @use_args({'model':fields.Str(validate=lambda x:x in models.keys(),
                required=True),
            'per_page':fields.Int(missing=100)},
            location='query')
    def get(self,args):
        total = models[args['model']].query.count()
        pages = ceil(total/args['per_page'])
        return PaginationSchema().dump(dict(total=total,pages=pages))
            
